// components/register/index.js
import {
  CityList
} from '../wx-city-picker/pca.js';
//导入验证js
import WxValidate from "../../pages/unity/WxValidate.js";
var  util  = require( "../../pages/unity/common.js");

const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    matchinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._matchinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    },
    Userinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._UserinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    error:'',
    date: '',
    codes: [],
    city: '',
    citylist: CityList,
    form: { //增加form子元素
      birthday: "",
      championcategory: "",
      city: "",
      clubpush: "",
      comptioncategory: "",
      comptiongroup: "",
      idcard: "",
      mail: "",
      name: "",
      parentname: "",
      parentphoneno: "",
      parentstype: "",
      phoneno: "",
      sex: "",
      school: "",
      stationchampion: "",
      stationremark: "",
      stationremark2: "",
      streat: ""

    },
    focuslist:new Object()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSelect(e) {
      this.setData({
        codes: e.detail.code,
        city: e.detail.value
      })
    },

    _errorshow(msg){
      
       this.triggerEvent('_errorshow', {
        msg: msg
      }) // 将参数的形式传递给父组件
    },

    _UserinfopropertyChange: function (newVal, oldVal) {
      this.initValidateuseerinfo();
      this.setData({
        form: newVal,
        city: newVal.city,
        date: newVal.birthday
      });
      console.log("this.data.form");
      console.log(this.data.form);
    },
    ///报名提交方法
    _registerformSubmit(e) {
      var formdata = e.detail.value;
      console.log(formdata);
      if (!this.WxValidate.checkForm(formdata)) {
        //表单元素验证不通过，此处给出相应提示

        let error = this.WxValidate.errorList[0];
        console.log(error.param);
       
        

        switch (error.param) {
          case "name":

            //TODO
            break;
          case "sex":
            //TODO
            break;
          case "birthday":
            //TODO
            break;
          case "idcard":
            //TODO
            break;
          case "phoneno":
            //TODO
            break;
          case "mail":
            //TODO
            break;
          case "school":
            //TODO
            break;
          case "city":
            //TODO
            break;
          case "streat":
            //TODO
            break;
          case "parentname":
            //TODO
            break;
          case "parentphoneno":
            //TODO
            break;
          case "parentstype":
            //TODO
            break;
          case "comptiongroup":
            //TODO
            break;
          case "comptioncategory":
            //TODO
            break;
          case "clubpush":
            //TODO
            break;



        }
        console.log(util);
       // this.data.focuslist.removeChild();
        this._errorshow(error.msg);
        util.commontscrolltoview(error.param,this);
        
        return false;
      }

      if (this.data.matchinfo != undefined && this.data.matchinfo.matchid != undefined) {
        formdata.matchid = this.data.matchinfo.matchid;
      }
      console.log(formdata);
      // 微信小程序中是通过triggerEvent来给父组件传递信息的
      this.triggerEvent('_registerformSubmit', {
        formdata: formdata
      }) // 将参数的形式传递给父组件

    },
    _matchinfopropertyChange(newVal, oldVal) {
      this.initValidate();



    },

  

    filldata: function (userinfo) {
      var form = { //增加form子元素
        birthday: userinfo.birthday,
        championcategory: userinfo.championcategory,
        city: userinfo.city,
        clubpush: userinfo.clubpush,
        comptioncategory: userinfo.comptioncategory,
        comptiongroup: userinfo.comptiongroup,
        idcard: userinfo.idcard,
        mail: userinfo.mail,
        name: userinfo.name,
        parentname: userinfo.parentname,
        parentphoneno: userinfo.parentphoneno,
        parentstype: userinfo.parentstype,
        phoneno: userinfo.phoneno,
        sex: userinfo.sex,
        school: userinfo.school,
        stationchampion: userinfo.stationchampion,
        stationremark: userinfo.stationremark,
        stationremark2: userinfo.stationremark2,
        streat: userinfo.streat

      }

      this.setData({
        form: form,
        date: userinfo.birthday,
        city: userinfo.city
      })

      console.log("this.data.form");
      console.log(this.data.form);
      this.initValidate();
    },


    bindDateChange(e) {
      this.setData({
        date: e.detail.value
      });
      this.data.form.birthday = e.detail.value;
    },
    bindRegionChange(e) {
      this.setData({
        city: e.detail.value
      });
      this.data.form.city = e.detail.value;
    },

    initValidateuseerinfo() {
      let rules = {
        name: {
          required: true,
          maxlength: 10
        },
        sex: {
          required: true
        },
        birthday: {
          required: true,
          dateISO: true,
        },
        idcard: {
          required: true,
          idcard: true
        },
        phoneno: {
          required: true,
          tel: true
        },
        mail: {
          required: true,
          email: true
        },
        school: {
          required: true,
          maxlength: 20
        },
        city: {
          required: true
        },
        streat: {
          required: true
        },
        parentname: {
          required: true,
          maxlength: 10
        },
        parentphoneno: {
          required: true,
          tel: true
        },
        parentstype: {
          required: true
        }
      }

      let message = {
        name: {
          required: '请输入姓名',
          maxlength: '名字不能超过10个字'
        },
        idcard: {
          required: "请输入身份证号码",
          idcard: "请输入正确的身份证号码"
        },
        mail: {
          required: "请输入电子邮箱",
          email: "请输入正确的电子邮箱"
        },
        phoneno: {
          required: "请输入联系手机号码",
          tel: "请输入正确的手机号码"
        },
        sex: {
          required: "请选择您的性别",
        },
        birthday: {
          required: "请选择出生年月",
          dateISO: "请选择出生年月",
        },
        school: {
          required: '请输入学校名称',
          maxlength: '学校名称不能超过20个字'
        },
        city: {
          required: "请选择您的城市地区",
        },
        streat: {
          required: "请输入详细街道信息",
        },
        parentname: {
          required: '请输入监护人姓名',
          maxlength: '监护人名字不能超过10个字'
        },
        parentphoneno: {
          required: "请输入监护人联系手机号码",
          tel: "请输入正确的手机号码"
        },
        parentstype: {
          required: "请选择监护人关系",
        }
      }
      //实例化当前的验证规则和提示消息
      this.WxValidate = new WxValidate(rules, message);
    },

    initValidate() {
      let rules = {
        name: {
          required: true,
          maxlength: 10
        },
        sex: {
          required: true
        },
        birthday: {
          required: true,
          dateISO: true,
        },
        idcard: {
          required: true,
          idcard: true
        },
        phoneno: {
          required: true,
          tel: true
        },
        mail: {
          required: true,
          email: true
        },
        school: {
          required: true,
          maxlength: 20
        },
        city: {
          required: true
        },
        streat: {
          required: true
        },
        parentname: {
          required: true,
          maxlength: 10
        },
        parentphoneno: {
          required: true,
          tel: true
        },
        parentstype: {
          required: true
        },
        comptiongroup: {
          required: true
        },
        comptioncategory: {
          required: true
        },
        clubpush: {
          required: true
        },
      }

      let message = {
        name: {
          required: '请输入姓名',
          maxlength: '名字不能超过10个字'
        },
        idcard: {
          required: "请输入身份证号码",
          idcard: "请输入正确的身份证号码"
        },
        mail: {
          required: "请输入电子邮箱",
          email: "请输入正确的电子邮箱"
        },
        phoneno: {
          required: "请输入联系手机号码",
          tel: "请输入正确的手机号码"
        },
        sex: {
          required: "请选择您的性别",
        },
        birthday: {
          required: "请选择出生年月",
          dateISO: "请选择出生年月",
        },
        school: {
          required: '请输入学校名称',
          maxlength: '学校名称不能超过20个字'
        },
        city: {
          required: "请选择您的城市地区",
        },
        streat: {
          required: "请输入详细街道信息",
        },
        parentname: {
          required: '请输入监护人姓名',
          maxlength: '监护人名字不能超过10个字'
        },
        parentphoneno: {
          required: "请输入监护人联系手机号码",
          tel: "请输入正确的手机号码"
        },
        parentstype: {
          required: "请选择监护人关系",
        },
        comptiongroup: {
          required: "请选择参赛组别",
        },
        comptioncategory: {
          required: "请选择参赛渠道",
        },
        clubpush: {
          required: "请选择是否使用球场手推车",
        },
      }
      //实例化当前的验证规则和提示消息
      this.WxValidate = new WxValidate(rules, message);
    },
  },

})